package com.kxr.day09.webmagic.pipeline;

import com.kxr.day09.dao.NewsDao;
import com.kxr.day09.entity.News;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.sql.*;
import java.util.Date;

public class NewsPipeline implements Pipeline {

    private final NewsDao newsDao = new NewsDao();

    @Override
    public void process(ResultItems resultItems, Task task) {
        // 构建好实体类，这个实体类就是和数据库11对应的
        News news = new News();
        news.setTitle(resultItems.get("title"));
        news.setTime(resultItems.get("time"));
        news.setContent(resultItems.get("content"));
        news.setUrl(resultItems.getRequest().getUrl());
        news.setContentHtml(resultItems.get("contentHtml"));
        news.setAuthor(resultItems.get("author"));
        news.setCategory(resultItems.get("category"));
        news.setSource(resultItems.get("source"));
        // 执行保存操作
        // 我先在要去爬取数据是不是就要把数据库删掉，然后爬取，否则会有重复的数据
        // 判断一下，url在数据库里是否存在，如果不存在，在进行爬取
        if (!newsDao.exist(news.getUrl())) {
            newsDao.save(news);
        }
    }
}
